package com.fulihui.wgj.consumer.communication.normal;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.Map;

/**
 * 事件处理器容器注册类
 *
 * @author: Levon
 * @version: v 0.1 2018-04-26 11:16
 */
@Component
@Slf4j
public class NormalMsgProcessorRegister extends ApplicationObjectSupport {

    private static Map<String, NormalMsgProcessor> CONTAINER = Collections.emptyMap();

    @PostConstruct
    private void init() {
        Map<String, NormalMsgProcessor> beans = getApplicationContext()
                .getBeansOfType(NormalMsgProcessor.class);
        if (!CollectionUtils.isEmpty(beans)) {
            Map<String, NormalMsgProcessor> tmp = Maps.newHashMap();
            for (NormalMsgProcessor processor : beans.values()) {
                tmp.put(processor.getEvent(), processor);
            }
            CONTAINER = tmp;
            log.info("常规消息事件处理器 init = {}",CONTAINER);
        }
    }

    public NormalMsgProcessor getProcessor(String event) {
        return CONTAINER.get(event);
    }
}